let eventAgent = {
    events: (function () {
        if ((/AppleWebKit.*Mobile.*/).test(window.navigator.userAgent)) {
            return {
                type: "mobile",
                down: "touchstart",
                move: "touchmove",
                up: "touchend"
            };
        } else {
            return {
                type: "pc",
                down: "mousedown",
                move: "mousemove",
                up: "mouseup"
            };
        }
    })(),
    onmove() {
        this.dom.addEventListener(eventAgent.events["move"], eventAgent._move);
        return this;
    },
    ondown() {
        this.dom.addEventListener(eventAgent.events["down"], eventAgent._down);
        return this;
    },
    onup() {
        this.dom.addEventListener(eventAgent.events["up"], eventAgent._up);
        return this;
    },
    type: function () {
        return eventAgent.events.type;
    },
    _down: function (e) {
        let ths = e.currentTarget['[touch]'];
        e.action = "down";
        ths.direction = "none";
        e.direction = ths.direction;
        ths.isdown = true;
        ths.ismove = false;
        ths.movefirst = true;
        ths.itime = new Date().getTime();

        ths.xis = e.touches ? e.touches[0].pageX : e.pageX;
        ths.yis = e.touches ? e.touches[0].pageY : e.pageY;
        ths.cxis = ths.xis;
        ths.cyis = ths.yis;
        e.xis = ths.xis;
        e.yis = ths.yis;
        ths._x = ths.xis;
        ths._y = ths.yis;
        ths._ontouch(ths.dom, e);
    },
    _move: function (e) {
        let ths = e.currentTarget['[touch]'];
        if (ths.isdown) {
            e.action = "move";
            ths.ismove = true;
            ths.cxis = e.touches ? e.touches[0].pageX : e.pageX;
            ths.cyis = e.touches ? e.touches[0].pageY : e.pageY;
            e.xis = ths.cxis;
            e.yis = ths.cyis;
            e.offsetX = ths.cxis - ths.xis;
            e.offsetY = ths.cyis - ths.yis;
            if (ths.movefirst) {
                ths.movefirst = false;
                if (Math.abs(e.offsetX) > Math.abs(e.offsetY)) {
                    ths.oishv = "h";
                } else {
                    ths.oishv = "v";
                }
                if (ths.oishv === "h") {
                    if (ths.cxis > ths.xis) {
                        ths.odirection = "right";
                    } else {
                        ths.odirection = "left";
                    }
                } else {
                    if (ths.cyis > ths.yis) {
                        ths.odirection = "bottom";
                    } else {
                        ths.odirection = "top";
                    }
                }
            }
            let _offsetx = ths.cxis - ths._x;
            let _offsety = ths.cyis - ths._y;
            if (Math.abs(_offsetx) > Math.abs(_offsety)) {
                ths.ishv = "h";
            } else {
                ths.ishv = "v";
            }
            if (ths.ishv === "h") {
                if (ths.cxis > ths._x) {
                    ths.direction = "right";
                } else {
                    ths.direction = "left";
                }
            } else {
                if (ths.cyis > ths._y) {
                    ths.direction = "bottom";
                } else {
                    ths.direction = "top";
                }
            }
            e.direction = ths.direction;
            e.odirection = ths.odirection;
            ths._x = ths.cxis;
            ths._y = ths.cyis;
            ths._ontouch(ths.dom, e);
        }
    },
    _up: function (e) {
        let ths = e.currentTarget['[touch]'];
        if (ths.isdown) {
            e.action = "up";
            ths.isdown = false;
            e.timeLast = new Date().getTime() - ths.itime;
            e.offsetY = ths.cyis - ths.yis;
            e.offsetX = ths.cxis - ths.xis;
            e.ismove = ths.ismove;
            e.xis = ths.cxis;
            e.yis = ths.cyis;
            e.direction = ths.direction;
            e.odirection = ths.odirection;
            ths._ontouch(ths.dom, e);
            ths.ismove = false;
        }
    }
};

class Touch {
    constructor(dom, ontouch) {
        this.dom = dom;
        this._ontouch = ontouch;
        this.xis = 0;
        this.yis = 0;
        this.cxis = 0;
        this.cyis = 0;
        this.direction = "";
        this.isdown = false;
        this.ismove = false;
        this.itime = 0;
        this.movefirst = false;
        this.ishv = "h";
        this.dom.style.webkitUserSelect = "none";
        this.dom.style.webkitTouchCallout = "none";
        this.dom.style.webkitUserDrag = "none";
        this.dom.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
        this.dom['[touch]'] = this;
        eventAgent.ondown.call(this);
        eventAgent.onmove.call(this);
        eventAgent.onup.call(this);
    }

    ontouch(fn) {
        this._ontouch = fn;
        return this;
    };
}

export default Touch;